package learn.hibernate.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import learn.hibernate.bean.po.PoResource;
import learn.hibernate.bean.po.PoRole;
import learn.hibernate.dao.ResourceDaoI;
import learn.hibernate.dao.RoleDaoI;
import learn.hibernate.service.ResourceServiceI;

@Service
public class ResourceServiceImpl implements ResourceServiceI{

	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ResourceDaoI resourceDao;
	
	@Autowired
	private RoleDaoI roleDao;
	
	@Override
	public void saveSimpleResource(){
		PoResource poResource=new PoResource();
		poResource.setName("r1");
		resourceDao.save(poResource);
	}
	
	@Override
	public void saveResourceWithRole(){
		PoResource poResource=new PoResource();
		poResource.setName("r1");
		PoRole role=new PoRole();
		role.setName("L1");
		poResource.getRoleList().add(role);
		resourceDao.save(poResource);
	}
	
	@Override
	public void deleteResourceByHql(){
		String hql="delete from PoResource";
		resourceDao.executeHql(hql);
	}
	
	@Override
	public void deleteResourceByDaoDelete(int id){
		PoResource poResource=resourceDao.get(PoResource.class, id);
		resourceDao.delete(poResource);
	}
	
	@Override
	public void findResourceWithRole(int id){
	/*	String hql="select r from PoResource r where r.id=:id";
		Map<String, Object> parameterMap=new HashMap<String, Object>();*/
		PoResource poResource=resourceDao.get(PoResource.class, id);
		logger.info(poResource.toString());
		logger.info("************************");
		poResource.getRoleList();
		logger.info(poResource.getRoleList().get(0).toString());
	}
	
	@Override
	public void updateResourceWithRole(int id) {
		PoResource poResource=resourceDao.get(PoResource.class, id);
		poResource.setName("R update in resource");
		poResource.getRoleList().get(0).setName("L update in resource.");
		resourceDao.update(poResource);
	}
}
